import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '*',
        redirect: '/'
    },
    {
        path: '/',
        component: () => import('../views/系统首页.vue')
        // component: () => import('../views/Login.vue')
    },
    {
        path: '/products',
        name: '商品管理',
        component: () => import('../views/商品管理.vue')
        // component: () => (resolve) => require(['../views/商品管理.vue'], resolve),
        // meta: {
        //     isLogin: false
        // }
    },
    {
        path: '/data',
        name: '数据总览',
        component: () => import('../views/数据总览.vue')
        // component: () => (resolve) => require(['../views/数据总览.vue'], resolve),
        // meta: {
        //     isLogin: false
        // }
    },
    {
        path: '/user',
        name: '用户管理',
        component: () => import('../views/用户管理.vue')
        // component: () => (resolve) => require(['../views/订单管理.vue'], resolve),
        // meta: {
        //     isLogin: false
        // }
    },
    {
        path: '/order',
        name: '订单管理',
        component: () => import('../views/订单管理.vue')
        // component: () => (resolve) => require(['../views/订单管理.vue'], resolve),
        // meta: {
        //     isLogin: false
        // }
    }
    ,
    {
        path: '/login',
        name: '登录界面',
        component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
    }, {
        path: '/index',
        name: '系统首页',
        component: () => import('../views/系统首页.vue')
    }

]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    //获取用户登录成功后储存的登录标志
    let getFlag = localStorage.getItem("Flag");

    //如果登录标志存在且为isLogin，即用户已登录
    if (getFlag === "isLogin") {
        //设置vuex登录状态为已登录
        next()
        //如果登录标志不存在，即未登录
    } else {
        if (to.path == "/login") {
            next();
        } else {
            next({
                path: '/login',
            })
        }
        //用户想进入需要登录的页面，则定向回登录界面


        //用户进入无需登录的界面，则跳转继续


    }
})

export default router
